package com.ityy.ruiji.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ityy.ruiji.common.R;
import com.ityy.ruiji.dao.CategoryDao;
import com.ityy.ruiji.dao.SetmealDao;
import com.ityy.ruiji.dao.SetmealDishDao;
import com.ityy.ruiji.entity.*;
import com.ityy.ruiji.entity.dto.SetmealDto;
import com.ityy.ruiji.exception.CustomerException;
import com.ityy.ruiji.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired(required = false)
    private SetmealDao setmealDao;

    @Autowired(required = false)
    private SetmealDishDao setmealDishDao;

    @Autowired(required = false)
    private CategoryDao categoryDao;



    @Override
    @Transactional
    public R save(SetmealDto setmealDto) {
        //1. 补全时间
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDao.save(setmealDto);
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        setmealDishList.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setSort(0);
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateTime(setmealDto.getCreateTime());
            setmealDish.setUpdateTime(setmealDto.getUpdateTime());
            return setmealDish;
        }).collect(Collectors.toList());
        setmealDishDao.dishSave(setmealDishList);
        return R.success("添加成功");
    }

    @Override
    public R page(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);
        List<Setmeal> setmealList = setmealDao.page(name);
        PageInfo<Setmeal> pageInfo = new PageInfo(setmealList);
        List<SetmealDto> list = setmealList.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category byId = categoryDao.findById(setmealDto.getCategoryId());
            setmealDto.setCategoryName(byId.getName());
            return setmealDto;
        }).collect(Collectors.toList());


        Page<SetmealDto> page1 = new Page(list, pageInfo.getTotal());
        return R.success(page1);
    }

    @Override
    public R deleteByIds(List<Long> ids) {
        Long count = setmealDao.findCountByIds(ids);

        //2. 如果存在在售，抛出异常
        if(count>0){
            throw  new CustomerException("在售的套餐不能删除");
        }

        //3. 如果不存在在售我们则删除套餐
        setmealDao.deleteByIds(ids);

        //4. 删除套餐的菜品
        setmealDishDao.deleteBySetmealIds(ids);

        return R.success("删除成功");
    }

    @Override
    public R findById(Long categoryId, Integer status) {
       List<Setmeal> setmealList = setmealDao.findById(categoryId,status);
        return R.success(setmealList);
    }
}
